import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
import 'package:image_picker/image_picker.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'MS-toast-util.dart';

Future<List<String>> pickImages(BuildContext context, int max) async {
  var result = await showModalBottomSheet(
      context: context,
      builder: (BuildContext context) {
        return CupertinoActionSheet(
          actions: [
            InkWell(
              onTap: () {
                Navigator.pop(context, 0);
              },
              child: Container(
                color: const Color(0xFF63BD75),
                height: 48.w,
                width: double.infinity,
                alignment: Alignment.center,
                child:  Text("Gallery",style: TextStyle(fontSize: 20.sp,fontWeight: FontWeight.bold,color: Colors.white),),
              ),
            ),
            InkWell(
              onTap: () {
                Navigator.pop(context, 1);
              },
              child: Container(
                  color: const Color(0xFF63BD75),
                  height: 48.w,
                  width: double.infinity,
                  alignment: Alignment.center,
                  child: Text("Camera",style: TextStyle(fontSize: 20.sp,fontWeight: FontWeight.bold,color: Colors.white))),
            )
          ],
        );
      });

 if(result == 0){
   var status = await Permission.photos.status;
   if (status.isDenied) {
     if ((await Permission.photos.request()).isDenied) {
       showToast(
           "\"LifeMilestones\"  requires access to your album to post a record photo or update your personal avatar.");
       return [];
     }
   }
   return await _pickImages(ImageSource.gallery, max);
 }else{
   var status = await Permission.camera.status;
   if (status.isDenied) {
     if ((await Permission.camera.request()).isDenied) {
       showToast(
           "\"LifeMilestones\" requires access to your camera to take and upload record pictures");
       return [];
     }
   }
   return await _pickImages(ImageSource.camera, max);
 }

}

Future<List<String>> _pickImages(ImageSource source,int max)async{
  final ImagePicker picker = ImagePicker();
  if(source == ImageSource.gallery){
    final List<XFile> image = await picker.pickMultiImage(imageQuality: max);
    return image.map((e) => e.path).toList();
  }else{
    final XFile? image = await picker.pickImage(source: source);
    return image != null ? [image.path]:[];
  }
}
